//
//  JNTSearchTableViewController.m
//  CRSCMOA
//
//  Created by c on 16/8/12.
//  Copyright © 2016年 王文晓. All rights reserved.
//

#import "JNTSearchTableViewController.h"
#import "JNTDataRequestTools.h"
#import "UserListTableViewCell.h"

@interface JNTSearchTableViewController ()

@property (nonatomic, strong) NSMutableArray *allSearchArray;
@end

@implementation JNTSearchTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    NSString *url = @"http://111.206.82.216:8050/JBaas/user/findByName?authCode=APP*JNT@2015E0298TR";
    NSMutableDictionary *mdic = [NSMutableDictionary dictionaryWithObjectsAndKeys:self.searchStr,@"name",nil];
    
    [self fenchDataSourceWithUrlString:url parameter:mdic];
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"UserListTableViewCell" bundle:nil] forCellReuseIdentifier:@"UserListCell"];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete implementation, return the number of rows
    return self.allSearchArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UserListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UserListCell" forIndexPath:indexPath];
    NSDictionary *dic = self.allSearchArray[indexPath.row];
    cell.nameLabel.text = [dic objectForKey:@"name"];
    cell.phoneLabel.text = [dic objectForKey:@"phone"];
    return cell;
}

#pragma mark --请求数据--
- (void)fenchDataSourceWithUrlString:(NSString *)urlStr parameter:(NSMutableDictionary *)parameter {
    
    
    [JNTDataRequestTools postFetchDataWithUrlString:urlStr paramenters:parameter success:^(id data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        
        NSLog(@"我的订单-----%@", [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
        
        self.allSearchArray = dic[@"items"];
        [self.tableView reloadData];
    } fail:^{
        
        NSLog(@"通讯录请求失败");
    } view:self.tableView];
    
}


- (NSMutableArray *)allSearchArray {
    
    if (!_allSearchArray) {
        
        _allSearchArray = [[NSMutableArray alloc] init];
    }
    return _allSearchArray;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
